const fs = require('fs')
var _DIR,_POST
exports.main = (req,res) =>{
    _DIR = req.bind.dir
    _POST = req.bind.__POST
    if(_POST && 'y' == _POST['ajax']){
        delete _POST.ajax
        ajaxAction(_POST,res)
        return
    }
    const tool = require(_DIR+'/module/emma.js'),
        conf = require(_DIR+'/config/config.js')    //全局配置文件
    var HTML = tool.require('html')
    var h = new HTML();
    
    h.isMobile(req,tool)
    h.load({title:'系统运行日志',js:['/public/jquery-1.12.4.min','/public/js/plog'],css:'/public/css/plog'})
    h.appHead('<meta charset="utf-8">')
    var html = ''
    html += getDirInfo(conf.log)
    html += '<textarea id="display_cottent"></textarea>'

    h.appBody(html)
    res.write(h.getHtml())
}
//  获取目录信息
function getDirInfo(dir){
    var xhtml = '',
        ul,path,
        data = fs.readdirSync(_DIR+'/'+dir)
    for(var k in data){
        path = dir+'/'+data[k]
        if(fs.statSync(path).isDirectory()){
            ul = getDirInfo(path)
            if(ul) xhtml += `<li>${path}${ul}</li>`
        }
        else xhtml += `<li><a href="javascript:void(0);" class="file_get_ctt">${path}</a></li>`
    }
    if(xhtml) xhtml = `<div id="files_list"><ul>${xhtml}</ul></div>`
    return xhtml
}
//  ajax 请求
function ajaxAction(data,res){
    var item = data['item']? data['item']:''
    var ret = ''
    delete data.item
    if('file_get_ctt' == item){
        var name = _DIR+'/'+data['name']
        if(fs.existsSync(name)) ret = fs.readFileSync(name).toString()        
    }
    //if(data) res.write(JSON.stringify(data))
    res.write(ret)
}